package cn.sc.summer.feign.client.login;

import cn.sc.summer.constant.model.Result;
import cn.sc.summer.feign.config.FeignRequestConfig;
import cn.sc.summer.protocol.form.login.AppLoginForm;
import cn.sc.summer.protocol.form.login.LoginForm;
import cn.sc.summer.protocol.form.login.SmsCodeLoginForm;
import cn.sc.summer.token.constant.LoginAPIConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Map;

/**
 * 接口：用户登录接口信息
 *
 * @author a-xin
 * @date 2023/9/3 11:57
 */
@FeignClient(name = "${feign.client.server}", fallbackFactory = SysLoginClient.SysLoginFallback.class, configuration = FeignRequestConfig.class)
public interface SysLoginClient {

    /**
     * 用户名密码登录
     *
     * @param loginForm 登录信息表单
     * @return 登录token信息
     */
    @RequestMapping(value = "/auth" + LoginAPIConstant.LOGIN_API, method = RequestMethod.POST)
    Result<Map<String, Object>> login(@RequestBody LoginForm loginForm);

    /**
     * 手机验证码登录
     *
     * @param smsCodeLoginForm 登录信息表单
     * @return 登录token信息
     */
    @RequestMapping(value = "/auth" + LoginAPIConstant.SMS_LOGIN, method = RequestMethod.POST)
    Result<Map<String, Object>> smsLogin(@RequestBody SmsCodeLoginForm smsCodeLoginForm);

    /**
     * 手机小程序登录
     *
     * @param appLoginForm 登录信息表单
     * @return 登录token信息
     */
    @RequestMapping(value = "/auth" + LoginAPIConstant.APP_LOGIN, method = RequestMethod.POST)
    Result<Map<String, Object>> appLogin(@RequestBody AppLoginForm appLoginForm);

    /**
     * 登出处理
     */
    @RequestMapping(value = "/auth" + LoginAPIConstant.LOGOUT, method = RequestMethod.POST)
    void logout();

    @Slf4j
    @Component
    class SysLoginFallback implements FallbackFactory<SysLoginClient> {
        @Override
        public SysLoginClient create(Throwable cause) {
            log.error("Fallback reason:{}", cause.getMessage());
            return new SysLoginClient() {

                /**
                 * 用户名密码登录
                 *
                 * @param loginForm 登录信息表单
                 * @return 登录token信息
                 */
                @Override
                public Result<Map<String, Object>> login(LoginForm loginForm) {
                    return Result.fallback(cause.getMessage());
                }

                /**
                 * 手机验证码登录
                 *
                 * @param smsCodeLoginForm 登录信息表单
                 * @return 登录token信息
                 */
                @Override
                public Result<Map<String, Object>> smsLogin(SmsCodeLoginForm smsCodeLoginForm) {
                    return Result.fallback(cause.getMessage());
                }

                /**
                 * 手机小程序登录
                 *
                 * @param appLoginForm 登录信息表单
                 * @return 登录token信息
                 */
                @Override
                public Result<Map<String, Object>> appLogin(AppLoginForm appLoginForm) {
                    return Result.fallback(cause.getMessage());
                }

                /**
                 * 登出处理
                 */
                @Override
                public void logout() {
                    cause.printStackTrace();
                }
            };

        }
    }

}
